package in.abip.chisame;

import android.widget.Toast;

public class Waiter extends Thread {
	private long _lastUsed;
	private long _period;
	private boolean _halted;

	public Waiter(long period) {
		this._period = period;
		this._halted = false;
	}

	public void run() {
		long idle = 0;
		this.touch();
		do {
			idle = System.currentTimeMillis() - this._lastUsed;
			try {
				Thread.sleep(5000); // check every 5 seconds
			} catch (InterruptedException e) {
			}
			if (idle > this._period) {
				idle = 0;
				// do something here - e.g. call popup or so
	    		String s = "Idle.";
	    		Toast.makeText(
					null,
					s,
					Toast.LENGTH_LONG
				).show();
			}
		} while (!this._halted);
	}

	public synchronized void touch() {
		this._lastUsed = System.currentTimeMillis();
	}

	public synchronized void forceInterrupt() {
		this.interrupt();
	}

	/**
	 * Stop idle time monitoring.
	 */
	public void halt() {
		this._halted = true;
	}

	public synchronized void setPeriod(long period) {
		this._period = period;
	}

}
